package com.camelot.boot.module.system.service.companyrequirement;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import com.camelot.boot.module.system.controller.admin.companyrequirement.vo.*;
import com.camelot.boot.module.system.dal.dataobject.companyrequirement.CompanyRequirementDO;
import com.camelot.boot.framework.common.pojo.PageResult;
import com.camelot.boot.framework.common.pojo.PageParam;
import com.camelot.boot.framework.common.util.object.BeanUtils;

import com.camelot.boot.module.system.dal.mysql.companyrequirement.CompanyRequirementMapper;

import static com.camelot.boot.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.camelot.boot.module.system.enums.ErrorCodeConstants.*;

/**
 * 用户公司需求配置信息 Service 实现类
 *
 * @author 管理员
 */
@Service
@Validated
public class CompanyRequirementServiceImpl extends ServiceImpl<CompanyRequirementMapper,CompanyRequirementDO> implements CompanyRequirementService {

    @Resource
    private CompanyRequirementMapper companyRequirementMapper;

    @Override
    public Long createCompanyRequirement(CompanyRequirementSaveReqVO createReqVO) {
        // 插入
        CompanyRequirementDO companyRequirement = BeanUtils.toBean(createReqVO, CompanyRequirementDO.class);
        companyRequirementMapper.insert(companyRequirement);
        // 返回
        return companyRequirement.getId();
    }

    @Override
    public void updateCompanyRequirement(CompanyRequirementSaveReqVO updateReqVO) {
        // 校验存在
        validateCompanyRequirementExists(updateReqVO.getId());
        // 更新
        CompanyRequirementDO updateObj = BeanUtils.toBean(updateReqVO, CompanyRequirementDO.class);
        companyRequirementMapper.updateById(updateObj);
    }

    @Override
    public void deleteCompanyRequirement(Long id) {
        // 校验存在
        validateCompanyRequirementExists(id);
        // 删除
        companyRequirementMapper.deleteById(id);
    }

    private void validateCompanyRequirementExists(Long id) {
        if (companyRequirementMapper.selectById(id) == null) {
            throw exception(COMPANY_REQUIREMENT_NOT_EXISTS);
        }
    }

    @Override
    public CompanyRequirementDO getCompanyRequirement(Long id) {
        return companyRequirementMapper.selectById(id);
    }

    @Override
    public PageResult<CompanyRequirementDO> getCompanyRequirementPage(CompanyRequirementPageReqVO pageReqVO) {
        return companyRequirementMapper.selectPage(pageReqVO);
    }

}